var storage = storages.create("com.ncsj.tools.storages");
var r = GetSource()
storage.put("FWF", r[2])
storage.put("FWG", r[0])
storage.put("FWW", r[1])
let time1 = Date.parse(new Date());
storage.put("UPDATE", time1)

var i = 0;
while(true){
    i++
    try {
        addPower()
        sleep(1000)
        var object = className("android.widget.Image").depth(17).find();
        // console.log(object)
        if (!object.empty()) {
            object.forEach(function(currentValue, index) {
                clickUiBounds(className("android.widget.Button").text("OK"))
                var text=  className("android.widget.TextView").depth(19).find();
                text.forEach(function(k,index){
                    if( k.text()){
                        if( typeof k ){
                            var b = k.text().trim().replace(/\s*/g,"").split("/")
                            if(Number(b[0])/Number(b[1])<0.7){
                                clickUiBounds(className("android.widget.Button").text("Repair"))
                                sleep(5000)
                            }
                        }
                    }
                })
                var a = clickUiBounds(className("android.widget.Button").text("Mine"))
                if(a){
                    sleep(15000)
                }
                sleep(2000)
                currentValue.click()
                sleep(2000)
            })

        } 
        toast("等待中");
        sleep(4000)
    } catch (error) {
        
    }
    console.log(i)
    if(i%5==0){
        updateSource()
        sleep(1000)
    }
    click(1054,404)
    
}

function clickUiBounds(ui) {
    if (ui.exists()) {
        var a = ui.findOnce();
        if (a) {
            var b = a.bounds();
            if (b) {
                click(b.centerX(), b.centerY());
                return true;
            }
        }
    }
    return false;
}


function addPower(){
 // 体力判断
    try {
        
        var dep16=  className("android.widget.TextView").depth(16).find();
        var fish = Number(dep16[2].text());
        var power =0 
        var index1 = 0
        if(0<Number(dep16[3].text())){
            index1 = 3
            power = Number(dep16[3].text())
        }else{
            index1 = 4
            power = Number(dep16[4].text())
        }
        var power_max =dep16[4].text().trim().replace(/\s*/g,"").split("/")

        if(power <200){
            //增加体力
            var b =dep16[index1].bounds()
            if (b) {
                click(b.centerX()+100, b.centerY());
                sleep(1000)
                setText(3)
                if(fish >(power_max-power)/5){
                    setText(power_max-power)
                }else{
                    setText(((fish-1)*5).toFixed(0))
                }
                sleep(2000)
                className("android.widget.Button").text("Exchange").find()[0].click()
                
            }
            
        }
    } catch (error) {
        
    }
}

function GetSource() {
    var a = [0,0,0]
    var dep16=  className("android.widget.TextView").depth(16).find();
    try {
        a[0] = Number(dep16[0].text());
        a[1] = Number(dep16[1].text());
        a[2] = Number(dep16[2].text());
    } catch (error) {
        
    }
    return a
}

function updateSource(){
    var b = GetSource() 
    console.log(b)
    var FWG = storage.get("FWG")
    var FWW = storage.get("FWW")
    var FWF = storage.get("FWF")
    let time1 = Date.parse(new Date());
    if(b[0]==0&&b[1]==0&&b[2]==0){

    }else{
        if (FWG !=b[0]||FWW!=b[1]||FWF!=b[2]) {
            storage.put("FWF", b[2])
            storage.put("FWG", b[0])
            storage.put("FWW", b[1])
            storage.put("UPDATE", time1)
        }
    }
    
    console.log("now")
    console.log(time1)
    
    let old_time = storage.get("UPDATE")
    console.log("old")
    console.log(old_time)
    if((time1-old_time)/1000 >7500){
        SendCode()
        铃声(0, 1) 
    }
}

function 铃声(铃声类型, 是否循环播放, 播放时长) {
    var 播放时长 = 播放时长 || 10000
    var 是否循环播放 = 是否循环播放 || false
    if (是否循环播放) {
      播放时长 = 666 * 1000
    }
    var 铃声选择结果 = android.media.RingtoneManager.TYPE_NOTIFICATION
    switch (铃声类型) {
      case 0:
        铃声选择结果 = android.media.RingtoneManager.TYPE_RINGTONE
        break;
      case 1:
        铃声选择结果 = android.media.RingtoneManager.TYPE_ALARM
        break;
      case 2:
        铃声选择结果 = android.media.RingtoneManager.TYPE_ALL
        break;
      default:
        break;
    }
    var mp = new android.media.MediaPlayer();
    mp.setDataSource(context, android.media.RingtoneManager.getDefaultUri(铃声选择结果));
    if (是否循环播放) mp.setLooping(true);
    mp.prepare();
    mp.start();
    threads.start(function () {
      sleep(播放时长)
      if (mp.isPlaying()) {
        mp.stop()
      }
    });
    return mp;
}


 //短信通知
 function SendCode(){
    try {
        http.__okhttp__.setTimeout(15000);
        var Url = "http://49.235.204.147:9006/v1/slp/smscode"; 
        var ocr_Res = http.post(Url, {
            headers: {
                "Content - Type": "application/json;charset=UTF-8" //,
            },
        })
        
    } catch(e) {
        return 
    }
    
 }